<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	$gid = x7in::read('gid','i');
	$GROUP = new lib_group($gid);
	if(!$GROUP->is_valid()){
		include('./sources/admin/managegroups.php');
		return;
	}
	
	$GLOBALS['OUTPUT']->gid = $gid;
	
	$section = x7in::read('section');
	if(empty($section) || !in_array($section,array('groupsettings','groupmembers','grouppermissions')))
		$section = 'groupsettings';
	
	$GLOBALS['OUTPUT']->master_section = 'management';
	$GLOBALS['OUTPUT']->minor_section = $GLOBALS['LANG']->lang_admin->$section;
	
	$menu = $menus[$GLOBALS['OUTPUT']->master_section];
	$menu[$GLOBALS['LANG']->lang_admin->editgroup] = 'header';
	$menu[$GLOBALS['LANG']->lang_admin->groupsettings] = './index.php?act=admin&page=editgroup&gid='.$gid;
	$menu[$GLOBALS['LANG']->lang_admin->groupmembers] = './index.php?act=admin&page=editgroup&section=groupmembers&gid='.$gid;
	$menu[$GLOBALS['LANG']->lang_admin->grouppermissions] = './index.php?act=admin&page=editgroup&section=grouppermissions&gid='.$gid;
	
	$GLOBALS['OUTPUT']->cpmenu = $menu;
	
	$errors = array();
	$defaults = array();
	
	switch($section){
		case 'groupsettings':
			
			$form = new form();
			
			// Obtain a list of groups
			$groups = array();
			$riterator = $GLOBALS['DATABASE']->get('groups');
			while($gdata = $riterator->get_next()){
				$groups[$gdata['name']] = $gdata['gid'];
			}
			
			$form->add_default('groupname',$GROUP->data_cache['name']);
			$form->add_default('groupimage',$GROUP->data_cache['img']);
			
			$add = x7in::read('groupname');
			if(!empty($add)){
				$add = $form->get_add_default('groupname');
				$img = $form->get_add_default('groupimage');
				
				if(isset($groups[$add]) && $add != $GROUP->data_cache['name']){
					$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->groupnameinuse;
				}else{
					$GROUP->data_cache['name'] = $add;
					$GROUP->data_cache['img'] = $img;
					$GROUP->commit_changes();
					$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->groupedited;
				}
			}
			
			$form->add_field('text','groupname',$GLOBALS['LANG']->lang_admin->groupname);
			$form->add_field('text','groupimage',$GLOBALS['LANG']->lang_admin->groupimage);
			
			$GLOBALS['OUTPUT']->form = $form->get_fields();
			
			echo tpl_groupsettings::groupsettings();
			return;
		break;
		case 'groupmembers':
			
			$groups = array();
			$riterator = $GLOBALS['DATABASE']->get('groups');
			while($gdata = $riterator->get_next()){
				$groups[$gdata['name']] = $gdata['gid'];
			}
			
			$members = array();
			$pager = new lib_pager();
			$riterator = $GLOBALS['DATABASE']->get('users');
			while($udata = $riterator->get_next()){
				$user = new lib_user($udata['uid']);
				if($user->is_valid() && $user->data_cache['group'] == $gid && $pager->item()){
					$members[$user->data_cache['uid']] = $user;
				}
			}
			
			if(isset($_GET['exec'])){
				
				$newgroup = x7in::read('group','i');
				if(!in_array($newgroup,$groups) || $newgroup == $gid){
					$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->invalid_group;
				}else{
					foreach($_POST as $key=>$val){
						if(strpos($key,'user_') === 0){
							$user = new lib_user($val);
							if($user->is_valid() && $user->data_cache['group'] == $gid){
								$user->data_cache['group'] = $newgroup;
								$user->commit_changes();
								unset($members[$user->data_cache['uid']]);
							}
						}
					}
				}
				
			}
			
			$GLOBALS['OUTPUT']->members = $members;
			$GLOBALS['OUTPUT']->page = $pager->get_page();
			$GLOBALS['OUTPUT']->pages = $pager->get_page();
			$GLOBALS['OUTPUT']->groups = $groups;
			
			echo tpl_groupsettings::groupmembers();
			return;
			
		break;
		case 'grouppermissions':
			// TODO: Decide whether submit buttons should be centored or left-aligned and imlement
			
			$valid_perms = array(	'sending_perms'=>array('SEND_GLOBAL_MESSAGE','SEND_SYS_MESSAGE','SEND_IM_MESSAGE','READ_UNVOICED_USERS','GLOBAL_VOICE','GLOBAL_ACCESS','BAN_IMMUNITY'),
						'roomaxs_perms'=>array('BECOME_INVISIBLE','SEE_INVISIBLE','ENTER_FULL_ROOM','VIEW_ROOM_LOG'),
						'roomctrl_perms'=>array('BAN_KICK_USER','MUTE_USER','VIEW_IP','CREATE_ROOM','SET_ROOM_LOG','EDIT_ROOM_SETTINGS','EDIT_ROOM_PERMS','EDIT_ROOM_ACCESS'),
						'other_perms'=>array('OVERRIDE_DISABLE','VIEW_HIDDEN_EMAILS'),
						'user_perms'=>array('LOG_PM','SEND_OFFLINE_MESSAGE','USE_BUDDY_LIST'),
						'admin_perms'=>array('ACP_MANAGEMENT','ACP_EDITUSER_USERPERMISSIONS','ACP_UPDATEMANAGER','ACP_MANAGEBANS','ACP_MANAGEGROUPS','ACP_MANAGEROOMS','ACP_SETTINGS','ACP_INTEGRATION','ACP_APPEARANCE','ACP_MANAGELANG'),
						);
						
			$GLOBALS['OUTPUT']->forms = array();
			
			foreach($valid_perms as $perm_section=>$pinfo){
				
				$form = new form();
				
				foreach($pinfo as $permname){
					$form->add_field('select',$permname,$GLOBALS['LANG']->lang_admin->$permname,'',array($GLOBALS['LANG']->lang_admin->allow=>1,$GLOBALS['LANG']->lang_admin->inherit=>0,$GLOBALS['LANG']->lang_admin->deny=>-1));
					
					if(isset($_GET['update'])){
						$GROUP->set_permission($permname,(int)$_POST[$permname]);
					}
					
					$form->add_default($permname,$GROUP->local_permission_value($permname));
				
					$GLOBALS['OUTPUT']->forms[$GLOBALS['LANG']->lang_admin->$perm_section] = $form->get_fields();
				}
			}
			
			if(isset($_GET['update'])){
				$GROUP->commit_changes();
				$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->groupedited;
			}
			
			echo tpl_groupsettings::grouppermissions();
			return;
		break;
	}
	
?>